package stringbuilder01;
/**
 * 需求:
 * 已知一个整型数组
 * int[] arr={15,17,21,32}
 * 将这个数组以以下格式打印
 * "[15, 17, 21, 32]"
 * 利用拼接的一种思想: + / StringBuilder
 * JDK9 以前版本使用+拼接效率没有StringBuilder高
 * JDK9 以后版本使用哪种方式都可以
 */
public class StringBuilderDemo06 {
    public static void main(String[] args) {
        //method01();
        StringBuilder stringBuilder = new StringBuilder("[");
        int[] arr = {15,17,21,32};
        for (int i = 0; i < arr.length; i++) {
            stringBuilder.append(arr[i]);
            if (i != arr.length - 1) {
                stringBuilder.append(", ");
            }
        }
        stringBuilder.append("]");
        System.out.println(stringBuilder);
    }

    private static void method01() {
        int[] arr = {15,17,21,32};

        String str = "[";

        for (int i = 0; i < arr.length; i++) {
            /* if (i == arr.length - 1) {
                str += arr[i];
            }else{
                str += arr[i] + ", ";
            }*/
            str += arr[i];
            if (i!= arr.length - 1) {
                str += ", ";
            }
        }
        str += "]";
        System.out.println(str);
    }
}
